package leetcode61_70;

/**
 * @author shhjiang_001
 * @create 2020-06-20 22:52
 */

        /*
        给定一个包含非负整数的 m x n 网格，请找出一条从左上角到右下角的路径，使得路径上的数字总和为最小。
        说明：每次只能向下或者向右移动一步。
        示例:
        输入:
        [
        [1,3,1],
        [1,5,1],
        [4,2,1]
        ]
        输出: 7
        */

public class Solution64 {
    public int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        for (int i = 1; i < m; i++) {
            grid[i][0] = grid[i-1][0] + grid[i][0];
        }
        for (int i = 0; i < n; i++) {
            grid[0][i] = grid[0][i-1] + grid[0][i];
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                grid[i][j] = Math.min(grid[i-1][j],grid[i][j-1]) + grid[i][j];
            }

        }
        return grid[m-1][n-1];
    }
}
